package com.sky.service.impl;

import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishFlavorsMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorsMapper dishFlavorsMapper;

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {

        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());

        Dish dish = new Dish();
        BeanUtils.copyProperties(dishPageQueryDTO, dish);
        List<DishVO> dishVO = dishMapper.page(dish);
        return new PageResult(dishVO.size(), dishVO);
    }

    /**
     * 新增菜品
     *
     * @param dishDTO
     */
    @Override
    @Transactional
    public void addDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);

        dishMapper.addDish(dish);

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors.size() > 0 && flavors != null) {
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(dish.getId()));
            dishFlavorsMapper.addFlavors(flavors);
        }
    }

    /**
     * 修改菜品状态
     * @param status
     * @param id
     */
    @Override
    public void updateStatus(Integer status, Long id) {
        Dish dish=Dish.builder().id(id).status(status).build();
        dishMapper.update(dish);
        if(status.equals(StatusConstant.DISABLE)){
            List ids=new ArrayList();
            ids.add(id);
         List<Long>  setmealIds= setmealDishMapper.getSetmealIdsByDishIds(ids);
         if(setmealIds!=null &&setmealIds.size()>0){
             for (Long setmealId : setmealIds) {
                 Setmeal setmeal=Setmeal.builder().status(status).id(setmealId).build();
                 setmealMapper.update(setmeal);
             }
         }
        }
    }

    /**
     * 根据id查询菜品信息
     * @param id
     * @return
     */
    @Override
    public DishVO getById(Long id) {
         Dish dish=  dishMapper.getById(id);

        List<DishFlavor> flavors= dishFlavorsMapper.getById(id);
        DishVO dishVO=new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(flavors);
        return dishVO;
    }

    /**
     * 修改菜品信息
     * @param dishDTO
     */
    @Override
    @Transactional
    public void updateDish(DishDTO dishDTO) {
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);

        dishFlavorsMapper.deleteFlavors(dishDTO.getId());

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors!=null &&flavors.size()>0){
            flavors.forEach(flavor->{
                flavor.setDishId(dishDTO.getId());
            });
            dishFlavorsMapper.addFlavors(flavors);
        }

    }

    /**
     *   在售状态下，不可删除菜品;
     *   被套餐关联的菜品不能删除
     * @param ids
     */
    @Override
    public void deleteByIds(List<Long> ids) {

        //起售中的菜品不能删除
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            if(dish.getStatus().equals(StatusConstant.ENABLE)){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        //与套餐关联的菜品不能删除

        List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
        if(setmealIds!=null && setmealIds.size()>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        for (Long id : ids) {
            dishMapper.deleteById(id);
            dishFlavorsMapper.deleteFlavors(id);
        }

    }


    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @Override
    public List<DishVO> getDishVo(Long categoryId) {

        List<DishVO> dishVo = dishMapper.getDishVo(categoryId);

        return dishVo;

    }

    /**
     * C端-菜品浏览接口
     * @param dish
     * @return
     */

    @Override
    public List<DishVO> getDishById(Dish dish) {
        List<DishVO> dishVo  = dishMapper.getDishById(dish);

        for (DishVO vo : dishVo) {
            List<DishFlavor> flavors = dishFlavorsMapper.getById(vo.getId());
            vo.setFlavors(flavors);

        }

        return dishVo;
    }
}
